package com.sgb.kjwl.view.ui.activity.self.goods;

import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityOperationRecordBinding;
import com.sgb.kjwl.model.entity.MemberPriceEntity;
import com.sgb.kjwl.view.ui.adapter.self.goods.OperationRecodAdpater;
import com.sgb.kjwl.view.widget.self.goods.ShowVipPriceDetailWindow;
import com.sgb.kjwl.viewmodel.self.goods.OperationRecordViewModel;
import com.sgb.link.router.PathGoods;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.helper.OnMultiClickListener;

import java.util.LinkedHashMap;

@Route(path = PathGoods.OperationRecordActivity)
public class OperationRecordActivity extends BaseActivity<OperationRecordViewModel, ActivityOperationRecordBinding> {
    @Override
    protected int layoutId() {
        return R.layout.activity_operation_record;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(OperationRecordViewModel.class);
        mBinding.setVariable(BR.ViewModel, mViewModel);
    }

    private OperationRecodAdpater madpater;
    private ShowVipPriceDetailWindow mPopWindow;

    @Override
    protected void init() {
        initRecyclerView();
        initObserve();
        if (getIntent().getSerializableExtra("MemberPriceEntity") != null) {
            mViewModel.detail.postValue((MemberPriceEntity) getIntent().getSerializableExtra("MemberPriceEntity"));
        }
        mBinding.backView.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                finish();
            }
        });
    }

    private void initRecyclerView() {
        madpater = new OperationRecodAdpater(this);
        mBinding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerview.setAdapter(madpater);
        madpater.setLister(new OperationRecodAdpater.ChoiceData() {
            @Override
            public void ClickData(MemberPriceEntity.SkuMemberPriceList data, int position, MemberPriceEntity.ItemMemberList mdata) {
                mViewModel.mdata = mdata;
                mViewModel.getSkuMemberPriceDetail(data.getItemMemberNo(), data.getSkuId());
            }
        });
    }

    private void showPopWindow(LinkedHashMap<String, String> map) {
        if (mPopWindow == null) {
            mPopWindow = new ShowVipPriceDetailWindow(this);
        }
        mPopWindow.setBgView(mBinding.grayLayout);
        mPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        mPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        mPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                mPopWindow.dismiss();
            }
        });
        mPopWindow.updateData(map, "会员价详情", "", "关闭");
        mPopWindow.showDownPopwindow(mBinding.grayLayout, true);
    }


    private void initObserve() {

        mViewModel.detail.observe(this, data -> {
            if (data.getItemMemberList().size() == 0) {
                madpater.setDatasMayNull(data.getItemMemberList());
                mBinding.layoutNull.setVisibility(View.VISIBLE);
            } else {
                mBinding.layoutNull.setVisibility(View.GONE);
                mBinding.recyclerview.setVisibility(View.VISIBLE);
                madpater.setDatas(data.getItemMemberList());
            }
        });

        mViewModel.detailSku.observe(this, memberPriceSkuDetail -> {
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("规格属性", memberPriceSkuDetail.get(0).getSkuDetails());
            if (memberPriceSkuDetail.get(0).getParityPrice() != null && (!TextUtils.isEmpty(memberPriceSkuDetail.get(0).getPromotionPrice())) && !memberPriceSkuDetail.get(0).getPromotionPrice().equals("0.00"))
                map.put("销售价/促销价", "¥" + memberPriceSkuDetail.get(0).getParityPrice() + "/¥" + memberPriceSkuDetail.get(0).getPromotionPrice());
            if (memberPriceSkuDetail.get(0).getParityPrice() != null && (TextUtils.isEmpty(memberPriceSkuDetail.get(0).getPromotionPrice()) || memberPriceSkuDetail.get(0).getPromotionPrice().equals("0.00")))
                map.put("销售价", "¥" + memberPriceSkuDetail.get(0).getParityPrice());
            if (memberPriceSkuDetail.get(0).getParityPrice() == null && memberPriceSkuDetail.get(0).getPromotionPrice() != null)
                map.put("促销价", "¥" + memberPriceSkuDetail.get(0).getPromotionPrice());
            for (int i = 0; i < memberPriceSkuDetail.size(); i++) {
                switch (memberPriceSkuDetail.get(i).getLevel()) {
                    case 0:
                        map.put("普通会员", "¥" + memberPriceSkuDetail.get(i).getPrice());
                        break;
                    case 1:
                        map.put("一星会员", "¥" + memberPriceSkuDetail.get(i).getPrice());
                        break;
                    case 2:
                        map.put("二星会员", "¥" + memberPriceSkuDetail.get(i).getPrice());
                        break;
                    case 3:
                        map.put("三星会员", "¥" + memberPriceSkuDetail.get(i).getPrice());
                        break;
                    case 4:
                        map.put("四星会员", "¥" + memberPriceSkuDetail.get(i).getPrice());
                        break;
                    case 5:
                        map.put("五星会员", "¥" + memberPriceSkuDetail.get(i).getPrice());
                        break;
                }
            }
            map.put("发布人", TextUtils.isEmpty(mViewModel.mdata.getSupplierContact()) ? "/" : mViewModel.mdata.getSupplierContact());
            map.put("联系方式", TextUtils.isEmpty(mViewModel.mdata.getSupplierPhone()) ? "/" : mViewModel.mdata.getSupplierPhone());
            showPopWindow(map);
        });
    }


}
